package com.nsi.util;

import lombok.extern.slf4j.Slf4j;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Slf4j
public class EmailUtil {
    //公司服务器
    private static final String ALIDM_SMTP_HOST = "smtpdm.aliyun.com";
    private static final int ALIDM_SMTP_PORT = 25;

    /**
     * 新学说联合-用户注册
     * 用户注册方法
     *
     * @param toWho 发送方
     * @param code  随机码
     */
    public static void sendMail(String toWho, String code) throws Exception, MessagingException {

        final Properties props = new Properties();
        // 表示SMTP发送邮件，需要进行身份验证
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", ALIDM_SMTP_HOST);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.port", "465");

        // 发件人的账号
        props.put("mail.user", "service@mail.html9.top");
        // 访问SMTP服务时需要提供的密码
        props.put("mail.password", "Xinxueshuo123Mail");

        // 构建授权信息，用于进行SMTP进行身份验证
        javax.mail.Authenticator authenticator = new javax.mail.Authenticator() {
            @Override
            protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };

        // 使用环境属性和授权信息，创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
//		打开调试
//        mailSession.setDebug(true);
//		2、创建邮件对象
        Message message = new MimeMessage(mailSession);
        //发件人
        message.setFrom(new InternetAddress("service@mail.html9.top"));
        //收件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(toWho));
        //主题
        message.setSubject("新学说的激活邮件");

        message.setContent("<blockquote class=\"quote\" style=\"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex\">\n\t\t<div style=\"font-size:14px; color:#666;border:1px solid #d9d9d9; border-top:4px solid #215089; height:auto; padding:35px 35px 28px ;width:600px;margin:40px auto;font-family:microsoft yahei;word-wrap:break-word\">\n\t\t\t"
                + "<p style=\"margin: 0; background-color:#215089; height:30px; text-align:center\"><img src=\"http://data.xinxueshuo.cn/nsi/assets/img/index/logo.png\" alt=\"banner1\" width=\"120\" height=\"30\"/>\n\t\t\t<p style=\"margin: 0;\"><img src=\"http://data.xinxueshuo.cn/nsi/assets/img/swiper/banner07.jpg\" alt=\"banner2\" width=\"100%\" height=\"150\"/></p>\n\t\t\t"
                + "<p style=\"font-size:1.2em;font-weight:bold;font-size:14px;\">\u5C0A\u656C\u7684<span style=\"color:#000;\">" + toWho + "</span>\u60A8\u597D\uFF1A</p>\n\t\t\t<p style=\"line-height:10px;\">\u6B22\u8FCE\u60A8\u6CE8\u518C\u65B0\u5B66\u8BF4\u5E73\u53F0\u3002</p>\n\t\t\t<p style=\"line-height:10px;\">\u8BF7\u8BBF\u95EE\u4EE5\u4E0B\u94FE\u63A5\u5B8C\u6210\u60A8\u7684\u90AE\u7BB1\u6FC0\u6D3B\u9A8C\u8BC1\u3002</p>\n\t\t\t<p style=\"line-height:20px;\">"
                + "<h3><a href=\"http://data.xinxueshuo.cn/nsi-1.0/register?registerCode=" + code + "&UserMail=" + toWho + "\">http://data.xinxueshuo.cn/nsi-1.0/register?registerCode=" + code + "&UserMail=" + toWho + "</a></h3></p>\n\t\t\t<p style=\"line-height:20px;\">\u5982\u679C\u65E0\u6CD5\u6253\u5F00\u94FE\u63A5\uFF0C\u8BF7\u590D\u5236\u4E0A\u9762\u7684\u94FE\u63A5\u7C98\u8D34\u5230\u6D4F\u89C8\u5668\u7684\u5730\u5740\u680F\u3002 </p>\n\t\t\t<p style=\"color:#000;font-size:1.0em;font-weight:bold;font-size:14px;\">\u91CD\u8981\u8BF4\u660E\uFF1A</p>\n\t\t\t<p>\u60A8\u7684\u8D26\u53F7\u53EF\u4EE5\u5728\u65B0\u5B66\u8BF4\u56DB\u5E93\u5168\u4E66\u548C\u65B0\u5B66\u8BF4\u56FD\u9645\u6559\u80B2\u5B66\u9662\u5171\u540C\u4F7F\u7528\uFF0C\u671B\u8BF7\u77E5\u6653\uFF01</p>\n\t\t\t<dl style=\"background:#f9f9f9;  padding:12px; color:#8a6d3b; margin:25px 0;\">\n\t\t\t\t<dt style=\"line-height:30px; font-weight:bold; margin-left:12px\">\u65B0\u5B66\u8BF4\u53EF\u4EE5\u5E2E\u60A8\u505A\u4EC0\u4E48\uFF1F</dt>\n\t\t\t\t\t<dd style=\"line-height:30px; margin-left:12px \">\u6559\u80B2\u5708\u7684\u54A8\u8BE2\u52A8\u6001</dd>\n\t\t\t\t\t<dd style=\"line-height:30px; margin-left:12px \">\u4E13\u4E1A\u7684\u7814\u7A76\u6210\u679C</dd>\n\t\t\t\t\t<dd style=\"line-height:30px;  margin-left:12px\">\u56FD\u9645\u5B66\u6821\u54A8\u8BE2\u670D\u52A1</dd>\n\t\t\t\t\t<dd style=\"line-height:30px;  margin-left:12px\">\u5B66\u6821\u6570\u636E\u5E93\u6700\u8FD1\u66F4\u65B0</dd>\n\t\t\t\t\t<dd style=\"line-height:30px;  margin-left:12px\">\u6570\u636E\u53EF\u89C6\u5316\u5206\u5E03\u56FE</dd>\n\t\t\t</dl>\n\t\t\t<hr style=\"border: 1px dashed #ccc; width: 100%;\"/>\n\t\t\t<div style=\"font-size:14px; background-color:#f9f9f9; width:100%; height:300; text-align:center\">\n\t\t\t\t<div>\n\t\t\t\t\t<p style=\"color:#999; font-size:12px; line-height:12px; padding-top:20px;\">\u672C\u90AE\u4EF6\u7531\u7CFB\u7EDF\u81EA\u52A8\u53D1\u9001\uFF0C\u8BF7\u52FF\u76F4\u63A5\u56DE\u590D\uFF01\u5982\u6709\u4EFB\u4F55\u7591\u95EE\uFF0C\u8BF7\u8054\u7CFB\u6211\u4EEC\u7684\u5BA2\u670D\u4EBA\u5458\u3002</p>\n\t\t\t\t\t<p style=\"color:#999; font-size:12px; line-height:12px;\">\u8054\u7CFB\u7535\u8BDD\uFF1A\n\t\t\t\t\t\t<span style=\"border-bottom:1px dashed #ccc;z-index:1\" onclick=\"return false;\" >010-52468286</span></p>\n\t\t\t\t\t<p><img src=\"http://data.xinxueshuo.cn/nsi/assets/img/wechat_QR.png\" alt=\"banner3\" width=\"100\" height=\"100\" /></p>\n\t\t\t\t</div>\n\t\t\t</div>\n\t\t</div>\n\t</blockquote>", "text/html;charset=UTF-8");
        //		3、发送激活邮件
        Transport.send(message);
    }

    //	通用 通知邮件发送 ：通知用户 或 管理员
    public static void SendNotifyMail(String toWho, String Template) throws Exception, MessagingException {

        final Properties props = new Properties();
        // 表示SMTP发送邮件，需要进行身份验证
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", ALIDM_SMTP_HOST);
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.port", "465");
        // 发件人的账号
        props.put("mail.user", "service@mail.html9.top");

        // 访问SMTP服务时需要提供的密码
        props.put("mail.password", "Xinxueshuo123Mail");
        // 构建授权信息，用于进行SMTP进行身份验证
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // 用户名、密码
                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };

        // 使用环境属性和授权信息，创建邮件会话
        Session mailSession = Session.getInstance(props, authenticator);
//		打开调试
//      mailSession.setDebug(true);
//		2、创建邮件对象
        Message message = new MimeMessage(mailSession);
        //发件人
        message.setFrom(new InternetAddress("service@mail.html9.top"));
        //收件人
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(toWho));
        //主题
        message.setSubject("新学说通知：" + Template);
        //正文
        message.setContent("<blockquote class=\"quote\" style=\"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex\">\n<div style=\"font-size:14px; color:#666;border:1px solid #d9d9d9; border-top:4px solid #20a56e; height:auto; padding:12px 35px 28px ;width:600px;margin:40px auto;font-family:microsoft yahei;word-wrap:break-word\">\n<p style=\"line-height:20px;\">\n<span style=\"color:#000;font-size:1.2em;font-weight:bold;\">" + toWho + "</span>\uff0c\u6b22\u8fce\u60a8\u4f7f\u7528\u65b0\u5b66\u8bf4\u6570\u636e\u7cfb\u7edf\u3002\n</p>\n<p style=\"line-height:20px;\">\u4ee5\u4e0b\u662f\u901a\u77e5\u6d88\u606f</p>\n"
                + "<p style=\"line-height:20px;\"> <h3><br/> <a href=\"http://data.xinxueshuo.cn\">" + Template + "</a></h3></p>"
                + "\n<dl style=\"background:#f9f9f9; border:1px solid #dcdcdc; padding:12px; color:#8a6d3b; margin:25px 0;\">\n<dt style=\"line-height:30px; font-weight:bold; margin-left:12px\">\u65b0\u5b66\u8bf4\u53ef\u4ee5\u5e2e"
                + "\u60a8\u505a\u4ec0\u4e48\uff1f</dt>\n<dd style=\"line-height:30px; margin-left:12px \">\u6559\u80b2\u5708\u7684\u54a8\u8be2\u52a8\u6001</dd>\n<dd style=\"line-height:30px; margin-left:12px \">\u4e13\u4e1a\u7684\u7814"
                + "\u7a76\u6210\u679c</dd>\n<dd style=\"line-height:30px;  margin-left:12px\">\u56fd\u9645\u5b66\u6821\u54a8\u8be2\u670d\u52a1</dd></dl>\n<p style=\"color:#999; font-size:12px; line-height:12px; padding-top:24px;"
                + "border-top:1px solid #dcdcdc;\">\u672c\u90ae\u4ef6\u7531\u7cfb\u7edf\u81ea\u52a8\u53d1\u9001\uff0c\u8bf7\u52ff\u76f4\u63a5\u56de\u590d\uff01\u5982\u6709\u4efb\u4f55\u7591\u95ee\uff0c\u8bf7\u8054\u7cfb\u6211\u4eec\u7684\u5ba2"
                + "\u670d\u4eba\u5458\u3002</p>\n<p style=\"color:#999; font-size:12px; line-height:12px;\">\u65b0\u5b66\u8bf4 \u8054\u7cfb\u7535\u8bdd\uff1a<span style=\"border-bottom:1px dashed #ccc;z-index:1\" t=\"7\" onclick=\"return false;\" >010-52468286</span>"
                + "\n</p></div>\n</blockquote>", "text/html;charset=UTF-8");

        //		3、发送邮件
        Transport.send(message);
    }
}
